//
//  VideoTopupView.swift
//  NDAi
//
//  Created by 丸子 on 2024/7/2.
//

import UIKit

class VideoTopupView: UIView {
    let disposeBag = DisposeBag()
    var chooseDone: (() -> Void)?
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.text = "钻石余额不足，请充值"
        label.font = UIFont.pingFangSCRegular(ofSize: 18)
        label.textColor = .white
        label.textAlignment = .center
        return label
    }()
    
    lazy var confirmButton: UIButton = {
        let button = UIButton()
        button.setTitle("立即充值", for: .normal)
        button.setTitleColor(Theme.color.text_d1, for: .normal)
        button.titleLabel?.font = UIFont.pingFangSCRegular(ofSize: 16)
        button.backgroundColor = UIColor(hexString: "#45FFFF")
        button.allRound(25)
        return button
    }()
    
    private lazy var alertView: UIView = {
        let view = UIView()
        view.backgroundColor = .clear
        view.layer.cornerRadius = 10
        view.layer.masksToBounds = true
        return view
    }()
    
    lazy var backIV: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "img_video_bg-1")
        imageView.contentMode = .scaleToFill
        imageView.clipsToBounds = true
        return imageView
    }()
    
    
    init() {
        super.init(frame: CGRect(x: 0, y: 0, width: SCREEN_WIDTH, height: SCREEN_HEIGHT))
        self.backgroundColor = UIColor(hexString: "#000000", transparency: 0.3)
        addSubview(alertView)
        bindEvent()
        initViews()
    }
    
    func bindEvent(){
        confirmButton.rx.tap.subscribe(onNext: { [weak self] _ in
            guard let self = self else { return }
            UIView.animate(withDuration: 0.3, animations: {
                self.alertView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
                self.alpha = 0
            },completion: { _ in
                self.removeFromSuperview()
                self.chooseDone?()
            })
        }).disposed(by: disposeBag)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initViews() {
        alertView.addSubviews([backIV, titleLabel, confirmButton])
        backIV.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(confirmButton.snp.top).offset(-32)
        }
        
        confirmButton.snp.makeConstraints { (make) in
            make.height.equalTo(50)
            make.bottom.equalToSuperview().offset(-35)
            make.leading.trailing.equalToSuperview().inset(80)
        }
        
        alertView.snp.makeConstraints { (make) in
            make.leading.trailing.equalToSuperview().inset(28)
            make.centerY.equalToSuperview()
            make.height.equalTo(GET_WIDTH(W: 300))
        }
        
        alertView.transform = CGAffineTransform(scaleX: 0.01, y: 0.01)
        UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.6, initialSpringVelocity: 0.5, options: .curveEaseInOut, animations: {
            self.alertView.transform = .identity
        }, completion: nil)
       
    }
     
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        self.removeFromSuperview()
    }
}
